package com.yb.guigu.pick.auth.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yb.guigu.pick.auth.bean.dto.response.SysUserDTO;
import com.yb.guigu.pick.auth.bean.vo.request.*;
import com.yb.guigu.pick.auth.entity.SysUser;
import com.yb.guigu.pick.auth.service.ISysUserService;
import com.yb.guigu.pick.security.bean.GuiguAbstractAuthenticationToken;
import com.yb.guigu.pick.security.bean.GuiguPickUser;
import com.yb.guigu.pick.security.bean.LoginSuccessResult;
import com.yb.guigu.pick.core.bean.GuiguResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 姚兵
 * @since 2023-05-30
 */
@RestController
@RequestMapping("/sysuser")
public class SysUserController {

    @Autowired
    ISysUserService sysUserService;

    @GetMapping("/list")
    public GuiguResult list() throws InterruptedException {
        return sysUserService.listUserDTO();
    }

    @PostMapping("/page")
    @PreAuthorize("hasAuthority('sys:api:user:page')")
    public GuiguResult page(@RequestBody GuiguPage<SysUserConditionVo> page) throws InterruptedException {
        return sysUserService.listUserDTO(page);
    }

    @GetMapping("/create")
    public GuiguResult create(){
        return sysUserService.create();
    }


    @PostMapping("/create")
    public GuiguResult createUser(@RequestBody SysUserCreateVo createVo){
        return sysUserService.createUser(createVo);
    }

    @PostMapping("/edit")
    public GuiguResult updateUser(@RequestBody SysUserUpdateVo updateVo){
        return sysUserService.updateUser(updateVo);
    }


    @GetMapping("/{userId}")
    public GuiguResult getOne(@PathVariable Integer userId){
        SysUser one = sysUserService.getOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getUserId, userId));
        SysUserDTO sysUserDTO = new SysUserDTO();
        BeanUtils.copyProperties(one,sysUserDTO);
        return GuiguResult.success(sysUserDTO);
    }


    @GetMapping("/userInfo/{userId}")
    public GuiguResult userInfo(@PathVariable Integer userId, GuiguAbstractAuthenticationToken authentication){

        GuiguPickUser guiguPickUser = authentication.getGuiguPickUser();

        if(!guiguPickUser.getUserId().equals(userId)){
            return GuiguResult.error("userId不匹配");
        }

        SysUser sysUser = sysUserService.getOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getUserId, userId));

        LoginSuccessResult successResult=new LoginSuccessResult();

        BeanUtils.copyProperties(sysUser,successResult);

        successResult.setLoginAccessToken(guiguPickUser.getLoginAccessToken());

        return GuiguResult.success(successResult);
    }


    @PostMapping("/password/modify")
    public GuiguResult updatePassword(@RequestBody ModifyPasswordRequest modifyPasswordRequest){
        return sysUserService.modifyPassword(modifyPasswordRequest);
    }



    @GetMapping("/role/{userId}")
    public GuiguResult userRole(@PathVariable Integer userId){
        return sysUserService.userRole(userId);
    }
}
